package com.fdb.efp.nls.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.nls.service.facade.SRoleRescRuleService;
import com.fdb.efp.nls.service.vo.SRoleRescRuleParamVO;
import com.fdb.efp.nls.service.vo.SRoleRescRuleVO;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class SRoleRescRuleController extends AbstractController  {
	
	@Autowired
	@Qualifier("sRoleRescRuleService")
	private SRoleRescRuleService sRoleRescRuleService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/role/resc/rules", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SRoleRescRuleVO>> querySRoleRescRuleAll(
			@RequestBody SRoleRescRuleVO vo) {		
		// 根据用户角色获取用户权限方案
		List<SRoleRescRuleVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);

		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sRoleRescRuleService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sRoleRescRuleService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sRoleRescRuleService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sRoleRescRuleService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/role/resc/rule/", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SRoleRescRuleVO> queryByPk() {		
		SRoleRescRuleVO inVo = new SRoleRescRuleVO();
		SRoleRescRuleVO outputVo = sRoleRescRuleService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sRoleRescRule
	 * @return
	 */
	@RequestMapping(value="/s/role/resc/rule", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SRoleRescRuleVO inSRoleRescRuleVo) {		
		int outputVo = sRoleRescRuleService.deleteByPk(inSRoleRescRuleVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSRoleRescRuleVo
	 * @return
	 */
	@RequestMapping(value="/s/role/resc/rule", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SRoleRescRuleVO inSRoleRescRuleVo) {		
		int outputVo = sRoleRescRuleService.updateByPk(inSRoleRescRuleVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSRoleRescRuleVo
	 * @return
	 */
	@RequestMapping(value="/s/role/resc/rule", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSRoleRescRule(@RequestBody SRoleRescRuleVO inSRoleRescRuleVo) {
		int outputVo = sRoleRescRuleService.insertSRoleRescRule(inSRoleRescRuleVo);
		return getResponseData(outputVo);
		
	}
	/**
	 * 查询 当前角色 和 资源 对应的资源操作码
	 * @return
	 */
	@RequestMapping(value="/s/role/resc/roleRescRules", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<String>> queryRoleRescRules(
			@RequestBody SRoleRescRuleVO inSRoleRescRuleVo) {
		List<String> roleRescRules = sRoleRescRuleService.queryRoleRescRules(inSRoleRescRuleVo);
		return getResponseData(roleRescRules);
	}
	
	@RequestMapping(value = "/s/role/resc/saveRule", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> saveRoleRescRule(
			@RequestBody SRoleRescRuleParamVO sRoleRescRuleParamVO
			) {
		
		setUserInfoToVO(sRoleRescRuleParamVO);
		Integer flag = sRoleRescRuleService.saveRoleRescRule(sRoleRescRuleParamVO);
		return getResponseData(flag);
	}
}
